package com.itheima.d7_thread_communication;

import java.util.concurrent.Callable;

public class Desk {
    public static boolean flag = false;
}

class Cook implements Runnable {
    @Override
    public void run() {
        while (true) {
            synchronized (Desk.class) {
                if (!Desk.flag) {
                    Desk.flag = true;
                    System.out.println("厨师做包子");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    Desk.class.notifyAll();
                    try {
                        Desk.class.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                } else {
                    System.out.println("厨师白跑一趟");
                }
            }
        }
    }
}

class Eat implements Runnable {
    @Override
    public void run() {
        while (true) {
            synchronized (Desk.class) {
                if (Desk.flag) {
                    Desk.flag = false;
                    System.out.println("吃货吃包子");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    Desk.class.notifyAll();
                    try {
                        Desk.class.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }else {
                    System.out.println("吃货白跑一趟");
                }
            }
        }
    }
}
